package club.janna.concurrency.example.atomic;

import club.janna.concurrency.annotation.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * AtomicReferenceFieldUpdater 例子
 * @Auther: guopanbo
 * @Date: 2018/7/22 11:44
 * @Description:
 */
@Slf4j
@ThreadSafe
public class AtomicReferenceUpdateExample {
    public static void main(String[] args) {
        AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(Counter.class, Integer.class, "count");
        Counter counter = new Counter();
        updater.compareAndSet(counter, 0, 1);
        updater.compareAndSet(counter, 0, 2);
        updater.compareAndSet(counter, 1, 5);
        updater.compareAndSet(counter, 2, 6);
        log.info("value = {}", counter.getCount());
    }

    private static class Counter {
        @Getter
        public volatile Integer count = 0;
    }
}
